package pl.edu.agh.inpema.components.analyzers.simpleIPAnalyzer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import pl.edu.agh.inpema.components.analyzers.simpleIPAnalyzer.data.SingleIpCounter;
import pl.edu.agh.inpema.core.components.AnalyzerUtil;
import pl.edu.agh.inpema.core.configuration.ConfigurationException;
import pl.edu.agh.inpema.core.dataBase.DataBase;
import pl.edu.agh.inpema.core.model.AnalyzeResult;
import pl.edu.agh.inpema.core.model.Analyzer;
import pl.edu.agh.inpema.core.queue.Queue;

public class SimpleIPAnalyzer implements Analyzer {

	private static Logger logger = Logger.getLogger(SimpleIPAnalyzer.class);
	
	private SimpleIPAnalyzerRunner runner;
	
	private AnalyzerUtil analyzerUtil;
	
	public SimpleIPAnalyzer() {
		this.runner = new SimpleIPAnalyzerRunner();
	}
	
	public void startAnalyzer() {
		runner.start();
	}
	
	public boolean stopAnalyzer() {
		this.runner.setRunningFalse();
		return this.runner.isError();
	}
	
	public boolean isAnalyzerRunning() {
		return this.runner.isRunning();
	}
	
	public int compareResults(AnalyzeResult result1, AnalyzeResult result2) {
		List<Object> list1 = this.analyzerUtil.getResultObjects(result1.getAnalyzeResultDataId(), SingleIpCounter.class);
		Map<String, Integer> profile1Map = new HashMap<String, Integer>();
		for (Object obj : list1) {
			SingleIpCounter counter = (SingleIpCounter) obj;
			profile1Map.put(counter.getIp(), counter.getCounter());
		}
		List<Object> list2 = this.analyzerUtil.getResultObjects(result2.getAnalyzeResultDataId(), SingleIpCounter.class);
		Map<String, Integer> profile2Map = new HashMap<String, Integer>();
		for (Object obj : list2) {
			SingleIpCounter counter = (SingleIpCounter) obj;
			profile2Map.put(counter.getIp(), counter.getCounter());
		}
		
		int data1Sum = 0;
		Map<String, Integer> data1Percent = new HashMap<String, Integer>();
		for (String ip : profile1Map.keySet()) {
			data1Sum += profile1Map.get(ip);
		}
		for (String ip : profile1Map.keySet()) {
			data1Percent.put(ip, 100 * profile1Map.get(ip) / data1Sum);
		}
		int data2Sum = 0;
		Map<String, Integer> data2Percent = new HashMap<String, Integer>();
		for (String ip : profile2Map.keySet()) {
			data2Sum += profile2Map.get(ip);
		}
		for (String ip : profile2Map.keySet()) {
			data2Percent.put(ip, 100 * profile2Map.get(ip) / data2Sum);
		}
		
		int result = 0;
		for (String ip : data1Percent.keySet()) {
			Integer percent1 = data1Percent.get(ip);
			Integer percent2 = data2Percent.get(ip);
			if (percent2 == null)
				continue;
			result += Math.min(percent1, percent2);
		}
		
		return result;
	}

	public Class[] getDataClasses() {
		Class[] dataClass = { SingleIpCounter.class };
		return dataClass;
	}
	
	public void setAnalyzerUtil(AnalyzerUtil analyzerUtil) {
		this.analyzerUtil = analyzerUtil;
		this.runner.setAnalyzerUtil(analyzerUtil);
	}
	
	public AnalyzerUtil getAnalyzerUtil() {
		return this.analyzerUtil;
	}
}
